package com.sskj.xyft.mine;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.TimePickerView;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xyft.MVPActivity;
import com.sskj.xyft.R;
import com.sskj.xyft.mine.data.AssetRecordBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

/**
 * 出入金记录
 *
 * @author Hey
 * Create at  2019/03/21
 */
public class AssetRecordActivity extends MVPActivity<AssetRecordPresenter> {
    private int page = 1;
    private int pageSize = 10;
    private int type;
    public static final int Recharge = 0;
    public static final int WithDraw = 1;

    private String startTime;
    private String endTime;

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.start_time)
    TextView startTimeText;
    @BindView(R.id.end_time)
    TextView endTimeText;
    @BindView(R.id.recordsView)
    RecyclerView recordsView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private BaseAdapter<AssetRecordBean> adapter;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


    @Override
    public int getLayoutId() {
        return R.layout.activity_asset_record;
    }

    @Override
    public AssetRecordPresenter getPresenter() {
        return new AssetRecordPresenter();
    }

    @Override
    public void initView() {
        type = getIntent().getIntExtra("type", 1);
        if (type == WithDraw) {
            toolbar.setTitle("出金查询");
        } else {
            toolbar.setTitle("入金查询");
        }
        toolbar.setLeftButtonOnClickListener(v -> finish());
        initRefreshLayout(smartRefreshLayout);
        recordsView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<AssetRecordBean>(R.layout.item_asset_records, null, recordsView, true) {
            @Override
            public void bind(ViewHolder holder, AssetRecordBean item) {
                holder.setText(R.id.name, item.getPrice() + "/人民币")
                        .setText(R.id.time, DateUtil.getDateFormat(Long.valueOf(item.getAddtime()) * 1000, DateUtil.PATTERN_J));
                if (type == WithDraw) { // 出金
                    if (item.getState() == 1) {  // 出金 1 待审核 2到账中 3已拒绝 4已到账  入金  1 未支付 2已支付
                        holder.setText(R.id.status, "待审核");
                    } else if (item.getState() == 2) {
                        holder.setText(R.id.status, "到账中");
                    } else if (item.getState() == 3) {
                        holder.setText(R.id.status, "已拒绝");
                    } else {
                        holder.setText(R.id.status, "已到账");
                    }
                } else {
                    if (item.getState() == 1) {  // 出金 1 待审核 2到账中 3已拒绝 4已到账  入金  1 未支付 2已支付
                        holder.setText(R.id.status, "未支付");
                    } else {
                        holder.setText(R.id.status, "已支付");
                    }
                }

            }
        };
        initTime();
    }

    private void initTime() {
        endTime = DateUtil.getCurrentDate(DateUtil.PATTERN_E);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.WEEK_OF_MONTH, -1);
        startTime = DateUtil.getDateFormat(ca.getTime(), DateUtil.PATTERN_E);
        startTimeText.setText(startTime);
        endTimeText.setText(endTime);
        ClickUtil.click(startTimeText, view -> showTimePicker(true));
        ClickUtil.click(endTimeText, view -> showTimePicker(false));
    }

    private void showTimePicker(boolean isStart) {
        TimePickerView pickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        pickerView.setOnTimeSelectListener(date -> {
            if (isStart) {
                startTimeText.setText(dateFormat.format(date));
            } else {
                if (DateUtil.isTrueDateTwo(startTimeText.getText().toString(), dateFormat.format(date))) {
                    endTimeText.setText(dateFormat.format(date));
                    mPresenter.getAssetRecord(type, getText(startTimeText), getText(endTimeText), page, pageSize);
                } else {
                    ToastUtils.show("截止日期小于起始日期，请重新选择");
                }
            }
        });
        pickerView.show();
    }

    @Override
    public void initData() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getAssetRecord(type, getText(startTimeText), getText(endTimeText), page, pageSize);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getAssetRecord(type, getText(startTimeText), getText(endTimeText), page, pageSize);
            }
        });
        mPresenter.getAssetRecord(type, getText(startTimeText), getText(endTimeText), page, pageSize);
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, AssetRecordActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public void setData(List<AssetRecordBean> data) {
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            if (adapter.getData().size() == 0) {
                return;
            }
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }
}
